/*
 * @Author: 孙文豪
 * @Description: 注册组件
 * @Date: 2022-12-15 14:09:24
 * @LastEditTime: 2023-02-14 16:53:53
 * @LastEditors: 孙文豪
 */
// 组件
// import Button from './package/Button/index.vue'
import DragBox from './package/DragBox/index.vue'
// import NumAnimation from './package/NumAnimation/index.vue'
// import TransitionHeight from "./package/TransitionHeight/index.vue";
// import Score from "./package/Score/index.vue";
// import SwitchButton from "./package/SwitchButton/index.vue";
// import FormTable from "./package/FormTable/index.vue";
import Pagination from "./package/Pagination/index.vue";
import DropDown from "./package/DropDown/index.vue";
// import RangeSelection from "./package/RangeSelection/index.vue";
// import DateSelection from "./package/DateSelection/index.vue";
// import Loading from "./package/Loading/index.vue";
// // 全局方法组件

import confirmBox from "./package/ConfirmBox_e/index.js";
import message from "./package/Message_e/index.js";
// 自定义指令
import moveTips from "./directives/MoveTips_v/index.js";

const componentPool = {
  // Button,
  DragBox,
  // NumAnimation,
  // TransitionHeight,
  // Score,
  // SwitchButton,
  // FormTable,
  Pagination,
  DropDown,
  // RangeSelection,
  // DateSelection,
  // Loading,
  ConfirmBox_e: confirmBox,
  Message_e: message,
  MoveTips_v: moveTips
}


/**
 * 批量引入组件、指令、事件
 * @param {*} app    vue实例
 * @param {*} options   按需引入的配置
 */
const install = (app, options) => {
  // options 按需引入
  // app.use(swhUi, {
  //   components: [
  //       'Button',
  //       'DragBox',
  //       'ConfirmBox',
  //       'MoveTips'
  //   ]
  // })
  if (options && options.components) {
    // 按需引入
    const components = options.components
    components.forEach((componentsName) => {   //循环配置数组
      for (let key in componentPool) {
        /**
         * 这里需要对配置的组件名称进行多类型添加
         * xxx标签组件
         * xxx_v自定义指令
         * xxx_e自定义事件组件
         * 组件包含任意一个就引入，多了类型不会出现重复名称
         */
        const allNameType = [componentsName, `${componentsName}_v`, `${componentsName}_e`]
        // 配置的名称和组件名称一致就引入
        if (allNameType.includes(key)) {
          registerHandle(app, key, componentPool[key])
        }
      }
    })
  } else {
    /**
     * 全部批量引入
     * app.use(SupTopUI)
     */
    for (let key in componentPool) {
      registerHandle(app, key, componentPool[key])
    }
    // 下面方式打包报错
    // componentPool.forEach((component,name) => {
    //   console.log(name)
    //   if (component.name) {
    //     registerHandle(app, component.name, component)
    //   }
    // });
  }
}

// 注册逻辑
const registerHandle = (app, name, com) => {
  if (name.includes('_v')) {
    // 引入自定义指令 判断后缀，注册时删除后缀
    app.directive(name.replace('_v', ""), com())
    //  app.directive(name.replace('_v', ""), com())
  } else if (name.includes('_e')) {
    // 引入事件，例如确认弹窗 判断后缀，注册时删除后缀
    window[name.replace('_e', "")] = com
    // app.config.globalProperties[name.replace('_e', "")] = com
  } else {
    // 引入标签组件
    app.component(`Swh${name}`, com);
  }
}
if (typeof window !== 'undefined' && window.Vue) {
  install(window.Vue)
}
// 导出UI组件基本信息
let uilist = []
for (let key in componentPool) {
  const item = componentPool[key]
  const obj = {
    uiName: key.replace(new RegExp('_v|_e'), ""),   //名字处理掉后缀
    uiProps: item.props   //组件参数（指令和事件获取不到props）
  }
  uilist.push(obj)
}

// 导出
export default {
  install,
  uilist
}

